/**
 * 
 */
package com.baijia.tianxiao.push.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.baijia.tianxiao.push.biz.channel.StandardChannelUtils;
import com.baijia.tianxiao.push.dao.mybatis.PushChannelConfigMapper;
import com.baijia.tianxiao.push.enums.DeviceTypeEnum;
import com.baijia.tianxiao.push.model.PushChannelConfig;
import com.baijia.tianxiao.push.service.PushChannelConfigService;

/**
 * @author leiruiqi
 *
 * 这里是管理推送各个平台对应的app信息，只有在这里配置了相关信息，推送才能成功
 * 
 * 定时从数据库加载最新信息，刷新进本地内容，使数据访问的效率最大化，并且有一定的是实时性
 */
@Service("pushChannelConfigService")
public class PushChannelConfigServiceImpl implements PushChannelConfigService{

	@Resource
	private PushChannelConfigMapper pushChannelConfigMapper;
	
	private Map<String,PushChannelConfig> pushChannelMap = new HashMap<String,PushChannelConfig>();
	
	private Map<String,List<PushChannelConfig>> pushChannelListMap = new HashMap<String,List<PushChannelConfig>>();
	
	@PostConstruct
	public void init(){
		refresh();
	}
	
	@Override
	public PushChannelConfig getByChannelTypeAndId(String channelType, DeviceTypeEnum deviceType,String appId,Integer developStatus) {
		return pushChannelConfigMapper.getByChannelTypeAndId(channelType,deviceType.name(),appId,developStatus);
	}

	
	@Scheduled(cron="0 * * * * ? ")//每分钟刷新1次
	@Override
	public void refresh() {
		Map<String,PushChannelConfig> tempMap = new HashMap<String,PushChannelConfig>();
		Map<String,List<PushChannelConfig>> tempListMap = new HashMap<String,List<PushChannelConfig>>();
		List<PushChannelConfig> appList = queryAllByStatus(1);
		for(PushChannelConfig app:appList){
			String key = buildKey(app.getChannelType() , app.getDeviceType(), app.getAppId(),app.getDevelopStatus());
			tempMap.put(key, app);
			
			List<PushChannelConfig> typeList = tempListMap.get(app.getChannelType());
			if(typeList == null){
				typeList = new ArrayList<PushChannelConfig>();
				tempListMap.put(app.getChannelType(), typeList);
			}
			typeList.add(app);
		}
		this.pushChannelMap = tempMap;
		this.pushChannelListMap = tempListMap;
		
		StandardChannelUtils.notifyChannelConfigChange();
		
	}
	private String buildKey(String channelType, String deviceType,String appId,Integer developStatus){
		return channelType+"|"+deviceType+"|"+appId+"|"+developStatus;
	}

	@Override
	public void save(PushChannelConfig pushChannelApp) {
		pushChannelConfigMapper.insert(pushChannelApp);
	}

	@Override
	public void updateOne(PushChannelConfig pushChannelApp) {

		pushChannelConfigMapper.updateByPrimaryKeySelective(pushChannelApp);
	}
	@Override
	public List<PushChannelConfig> queryAllByStatus(Integer status) {
		return pushChannelConfigMapper.queryAllByStatus(status);
	}
	
	@Override
	public PushChannelConfig getByChannelTypeAndIdByCache(String channelType,DeviceTypeEnum deviceType, String appId,Integer developStatus) {
		String key = buildKey(channelType ,deviceType.name(), appId,developStatus);
		return pushChannelMap.get(key);
	}

	@Override
	public PushChannelConfig getById(Long id) {
		return this.pushChannelConfigMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<PushChannelConfig> queryEeffectByChannelType(String channelType) {
		return this.pushChannelListMap.get(channelType);
	}

}
